package main

import (
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
	"github.com/tarm/serial"
	// "strconv"
	// "strings"
	"fmt"
	"log"
	"time"
)

func main() {
	var sendTE, receiveTE *walk.TextEdit

	c := &serial.Config{Name: "COM4", Baud: 115200, ReadTimeout: time.Second * 5}
	s, err := serial.OpenPort(c)
	if err != nil {
		log.Fatal(err)
	}

	MainWindow{
		Title:   "串口调试",
		MinSize: Size{600, 400},
		Layout:  VBox{},
		Children: []Widget{

			TextEdit{AssignTo: &receiveTE, ReadOnly: true},
			TextEdit{AssignTo: &sendTE},
			PushButton{
				Text: "发送",
				OnClicked: func() {

					n, err := s.Write([]byte(sendTE.Text()))
					if err != nil {
						log.Fatal(err)
					}
					fmt.Println(sendTE.Text())
					fmt.Println(n)

				},
			},
		},
	}.Run()

	defer s.Close()
}
